


import java.util.Calendar;
import java.util.Timer;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Arrays;

public class sort {

    public static void main(String[] args) {//冒泡排序

//        Date d1 = new Date();
//        SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String str = s1.format(d1);
//        System.out.println("排序前的时间"+str);


        int[] arr = new int[5000];
        for (int i = 0; i < 5000; i++) {
            arr[i] = (int) (Math.random()*5000);
        }
        long start = System.currentTimeMillis();
        extracted(arr);
        long end = System.currentTimeMillis();
        double time = end-start;
        System.out.println("开始："+start+" "+"结束"+(end-start));


//
//        Date dd = new Date();
//        String str2 = s1.format(dd);
//        System.out.println("排序后的时间"+str2);
    }

    private static void extracted(int[] arr) {
        boolean flag = false;//牺牲空间定义变量来让循环少走一次
        int t = 0;
        for (int i = 0; i < arr.length-1; i++) {//时间复杂度为O(n^2);

            for (int j = 0; j < arr.length-1-i; j++) {
                t++;//记录运行次数
                if (arr[j]> arr[j+1]){
                    flag = true;
                    arr[j] = arr[j]^ arr[j+1];
                    arr[j+1] = arr[j]^ arr[j+1];
                    arr[j] = arr[j]^ arr[j+1];

                }
            }
            if (!flag){break;}else {flag=false;}

        }
        System.out.println(Arrays.toString(arr));
        System.out.println(t);
    }
}
